home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Video / gethdr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-19  |  31.1 KB  |  1,151 lines

  1. /* gethdr.c, header decoding                                                */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. extern "C"
  34. {
  35. #include "global.h"
  36. }
  37. #include "..\Video\VideoWrapper.h"
  38. extern __int64 rawPTS[1024];
  39. extern VideoWrapper *myVideo;
  40. extern int gopPOS;
  41. extern int gopNum;
  42.  
  43. static __int64  PTS,SCR,outgoingPTS;
  44. /* private prototypes */
  45. static void sequence_header _ANSI_ARGS_((void));
  46. static void group_of_pictures_header _ANSI_ARGS_((void));
  47. static void picture_header _ANSI_ARGS_((void));
  48. static void extension_and_user_data _ANSI_ARGS_((void));
  49. static void sequence_extension _ANSI_ARGS_((void));
  50. static void sequence_display_extension _ANSI_ARGS_((void));
  51. static void quant_matrix_extension _ANSI_ARGS_((void));
  52. static void sequence_scalable_extension _ANSI_ARGS_((void));
  53. static void picture_display_extension _ANSI_ARGS_((void));
  54. static void picture_coding_extension _ANSI_ARGS_((void));
  55. static void picture_spatial_scalable_extension _ANSI_ARGS_((void));
  56. static void picture_temporal_scalable_extension _ANSI_ARGS_((void));
  57. static int  extra_bit_information _ANSI_ARGS_((void));
  58. static void copyright_extension _ANSI_ARGS_((void));
  59. static void user_data _ANSI_ARGS_((void));
  60. static void user_data _ANSI_ARGS_((void));
  61.  
  62.  
  63.  
  64.  
  65. /* introduced in September 1995 to assist spatial scalable decoding */
  66. static void Update_Temporal_Reference_Tacking_Data _ANSI_ARGS_((void));
  67. /* private variables */
  68. static int Temporal_Reference_Base = 0;
  69. static int True_Framenum_max  = -1;
  70. static int Temporal_Reference_GOP_Reset = 0;
  71.  
  72. #define RESERVED    -1 
  73. static double frame_rate_Table[16] =
  74. {
  75.   0.0,
  76.   ((23.0*1000.0)/1001.0),
  77.   24.0,
  78.   25.0,
  79.   ((30.0*1000.0)/1001.0),
  80.   30.0,
  81.   50.0,
  82.   ((60.0*1000.0)/1001.0),
  83.   60.0,
  84.  
  85.   RESERVED,
  86.   RESERVED,
  87.   RESERVED,
  88.   RESERVED,
  89.   RESERVED,
  90.   RESERVED,
  91.   RESERVED
  92. };
  93.  
  94.  
  95. /*
  96.  * decode headers from one input stream
  97.  * until an End of Sequence or picture start code
  98.  * is found
  99.  */
  100. int Get_Hdr()
  101. {
  102.   unsigned int code;
  103.  
  104.   for (;;)
  105.   {
  106.     /* look for next_start_code */
  107.     next_start_code();
  108.     code = Get_Bits32();
  109.   
  110.     switch (code)
  111.     {
  112.     case SEQUENCE_HEADER_CODE:
  113.       sequence_header();
  114.       break;
  115.     case GROUP_START_CODE:
  116.       group_of_pictures_header();
  117.       break;
  118.     case PICTURE_START_CODE:
  119.       picture_header();
  120.       /* Update gop position */ 
  121.       if(picture_structure==FRAME_PICTURE || Second_Field==0)
  122.       {
  123.         gopPOS++;
  124.           if(gopPreviousPos>=gopPOS){
  125.               gop_count=gopPreviousPos+1;
  126.               myVideo->ResetRawPTS();
  127.           }
  128.           //update gopPreviousPos for next picture
  129.           gopPreviousPos=gopPOS;
  130.           rawPTS[temporal_reference]= PTS;
  131.       }
  132.  
  133.       if(myVideo->SCRlocked)
  134.         myVideo->SCRlocked= (myVideo->previousPicSCR <= SCR );
  135.       myVideo->previousPicSCR = SCR;
  136.       if (picture_coding_type==P_TYPE || picture_coding_type==I_TYPE)
  137.       {
  138.         // the backward reference is going to be output
  139. /*        if(picture_structure==FRAME_PICTURE || Second_Field==0){
  140.             myVideo->p.outgoingPTS=myVideo->p.backwardPTS;
  141.             myVideo->p.outgoingSCR=myVideo->p.backwardSCR;
  142.         }
  143.         else{
  144.             myVideo->p.outgoingPTS= myVideo->p.backwardPTS ? myVideo->p.backwardPTS : myVideo->p.outgoingPTS;
  145.             myVideo->p.outgoingSCR= myVideo->p.backwardSCR ? myVideo->p.backwardSCR : myVideo->p.outgoingSCR;
  146.         }
  147. */
  148.         if(picture_structure==FRAME_PICTURE || Second_Field==0){
  149.  
  150.             myVideo->p.backward = myVideo->p.fordward;
  151.  
  152.             myVideo->p.fordward.PTS                = PTS;
  153.             myVideo->p.fordward.SCR                = SCR;
  154.             myVideo->p.fordward.progressive_frame  = progressive_frame;
  155.             myVideo->p.fordward.repeat_first_field = repeat_first_field;
  156.             myVideo->p.fordward.picture_structure  = picture_structure;
  157.             myVideo->p.fordward.top_field_first    = top_field_first;
  158.         }
  159.       }
  160.       if (picture_coding_type==B_TYPE)
  161.       {
  162.         if(picture_structure==FRAME_PICTURE || Second_Field==0){
  163.             myVideo->p.actual.PTS                = PTS;
  164.             myVideo->p.actual.SCR                = SCR;
  165.             myVideo->p.actual.progressive_frame  = progressive_frame;
  166.             myVideo->p.actual.repeat_first_field = repeat_first_field;
  167.             myVideo->p.actual.picture_structure  = picture_structure;
  168.             myVideo->p.actual.top_field_first    = top_field_first;
  169.         }
  170.       }
  171.  
  172.       return 1;
  173.       break;
  174.     case SEQUENCE_END_CODE:
  175.       return 0;
  176.       break;
  177.     default:
  178.       if (!Quiet_Flag)
  179.         fprintf(stderr,"Unexpected next_start_code %08x (ignored)\n",code);
  180.       break;
  181.     }
  182.   }
  183. }
  184.  
  185.  
  186. /* align to start of next next_start_code */
  187.  
  188. void next_start_code()
  189. {
  190.   /* byte align */
  191.   Flush_Buffer(ld->Incnt&7);
  192.   while (Show_Bits(24)!=0x01L)
  193.   //while (Show_Bits(32)!=0x000001B3)
  194.     Flush_Buffer(8);
  195. }
  196.  
  197.  
  198. /* decode sequence header */
  199.  
  200. static void sequence_header()
  201. {
  202.   int i;
  203.   int pos;
  204.   
  205.  
  206.   pos = ld->Bitcnt;
  207.   horizontal_size             = Get_Bits(12);
  208.   vertical_size               = Get_Bits(12);
  209.   aspect_ratio_information    = Get_Bits(4);
  210.   frame_rate_code             = Get_Bits(4);
  211.   bit_rate_value              = Get_Bits(18);
  212.   marker_bit("sequence_header()");
  213.   vbv_buffer_size             = Get_Bits(10);
  214.   constrained_parameters_flag = Get_Bits(1);
  215.  
  216.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  217.   {
  218.     for (i=0; i<64; i++)
  219.       ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  220.   }
  221.   else
  222.   {
  223.     for (i=0; i<64; i++)
  224.       ld->intra_quantizer_matrix[i] = default_intra_quantizer_matrix[i];
  225.   }
  226.  
  227.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  228.   {
  229.     for (i=0; i<64; i++)
  230.       ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  231.   }
  232.   else
  233.   {
  234.     for (i=0; i<64; i++)
  235.       ld->non_intra_quantizer_matrix[i] = 16;
  236.   }
  237.  
  238.   /* copy luminance to chrominance matrices */
  239.   for (i=0; i<64; i++)
  240.   {
  241.     ld->chroma_intra_quantizer_matrix[i] =
  242.       ld->intra_quantizer_matrix[i];
  243.  
  244.     ld->chroma_non_intra_quantizer_matrix[i] =
  245.       ld->non_intra_quantizer_matrix[i];
  246.   }
  247.  
  248. #ifdef VERBOSE
  249.   if (Verbose_Flag > NO_LAYER)
  250.   {
  251.     printf("sequence header (byte %d)\n",(pos>>3)-4);
  252.     if (Verbose_Flag > SEQUENCE_LAYER)
  253.     {
  254.       printf("  horizontal_size=%d\n",horizontal_size);
  255.       printf("  vertical_size=%d\n",vertical_size);
  256.       printf("  aspect_ratio_information=%d\n",aspect_ratio_information);
  257.       printf("  frame_rate_code=%d",frame_rate_code);
  258.       printf("  bit_rate_value=%d\n",bit_rate_value);
  259.       printf("  vbv_buffer_size=%d\n",vbv_buffer_size);
  260.       printf("  constrained_parameters_flag=%d\n",constrained_parameters_flag);
  261.       printf("  load_intra_quantizer_matrix=%d\n",ld->load_intra_quantizer_matrix);
  262.       printf("  load_non_intra_quantizer_matrix=%d\n",ld->load_non_intra_quantizer_matrix);
  263.     }
  264.   }
  265. #endif /* VERBOSE */
  266.  
  267. #ifdef VERIFY
  268.   verify_sequence_header++;
  269. #endif /* VERIFY */
  270.  
  271.   extension_and_user_data();
  272. }
  273.  
  274.  
  275.  
  276. /* decode group of pictures header */
  277. /* ISO/IEC 13818-2 section 6.2.2.6 */
  278. static void group_of_pictures_header()
  279. {
  280.  
  281.   myVideo->gopFlag=true;
  282.  
  283.   int pos;
  284.   gopPOS=-1;
  285.   gopNum++;
  286.   if (ld == &base)
  287.   {
  288.     Temporal_Reference_Base = True_Framenum_max + 1;     /* *CH* */
  289.     Temporal_Reference_GOP_Reset = 1;
  290.   }
  291.   pos = ld->Bitcnt;
  292.   drop_flag   = Get_Bits(1);
  293.   hour        = Get_Bits(5);
  294.   minute      = Get_Bits(6);
  295.   marker_bit("group_of_pictures_header()");
  296.   sec         = Get_Bits(6);
  297.   frame       = Get_Bits(6);
  298.   closed_gop  = Get_Bits(1);
  299.   broken_link = Get_Bits(1);
  300.  
  301.   //Reseting field status
  302.   Second_Field = 0;
  303. #ifdef VERBOSE
  304.   if (Verbose_Flag > NO_LAYER)
  305.   {
  306.     printf("group of pictures (byte %d)\n",(pos>>3)-4);
  307.     if (Verbose_Flag > SEQUENCE_LAYER)
  308.     {
  309.       printf("  drop_flag=%d\n",drop_flag);
  310.       printf("  timecode %d:%02d:%02d:%02d\n",hour,minute,sec,frame);
  311.       printf("  closed_gop=%d\n",closed_gop);
  312.       printf("  broken_link=%d\n",broken_link);
  313.     }
  314.   }
  315. #endif /* VERBOSE */
  316.  
  317. #ifdef VERIFY
  318.   verify_group_of_pictures_header++;
  319. #endif /* VERIFY */
  320.  
  321.   extension_and_user_data();
  322.  
  323. }
  324.  
  325.  
  326. /* decode picture header */
  327.  
  328. /* ISO/IEC 13818-2 section 6.2.3 */
  329. static void picture_header()
  330. {
  331.   int pos;
  332.   int Extra_Information_Byte_Count;
  333.  
  334.  
  335.  
  336.   PTS = myVideo->myPES.PTS;
  337.   SCR = myVideo->myPES.SCR;
  338.   /* unless later overwritten by picture_spatial_scalable_extension() */
  339.   ld->pict_scal = 0; 
  340.   
  341.   pos = ld->Bitcnt;
  342.   temporal_reference  = Get_Bits(10);
  343.  
  344.   picture_coding_type = Get_Bits(3);
  345.   vbv_delay           = Get_Bits(16);
  346.  
  347.   if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
  348.   {
  349.     full_pel_forward_vector = Get_Bits(1);
  350.     forward_f_code = Get_Bits(3);
  351.   }
  352.   if (picture_coding_type==B_TYPE)
  353.   {
  354.     full_pel_backward_vector = Get_Bits(1);
  355.     backward_f_code = Get_Bits(3);
  356.   }
  357.  
  358. #ifdef VERBOSE
  359.   if (Verbose_Flag>NO_LAYER)
  360.   {
  361.     printf("picture header (byte %d)\n",(pos>>3)-4);
  362.     if (Verbose_Flag>SEQUENCE_LAYER)
  363.     {
  364.       printf("  temporal_reference=%d\n",temporal_reference);
  365.       printf("  picture_coding_type=%d\n",picture_coding_type);
  366.       printf("  vbv_delay=%d\n",vbv_delay);
  367.       if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
  368.       {
  369.         printf("  full_pel_forward_vector=%d\n",full_pel_forward_vector);
  370.         printf("  forward_f_code =%d\n",forward_f_code);
  371.       }
  372.       if (picture_coding_type==B_TYPE)
  373.       {
  374.         printf("  full_pel_backward_vector=%d\n",full_pel_backward_vector);
  375.         printf("  backward_f_code =%d\n",backward_f_code);
  376.       }
  377.     }
  378.   }
  379. #endif /* VERBOSE */
  380.  
  381. #ifdef VERIFY
  382.   verify_picture_header++;
  383. #endif /* VERIFY */
  384.  
  385.   Extra_Information_Byte_Count = 
  386.     extra_bit_information();
  387.   
  388.   extension_and_user_data();
  389.  
  390.   /* update tracking information used to assist spatial scalability */
  391.   Update_Temporal_Reference_Tacking_Data();
  392. }
  393.  
  394. /* decode slice header */
  395.  
  396. /* ISO/IEC 13818-2 section 6.2.4 */
  397. int slice_header()
  398. {
  399.   int slice_vertical_position_extension;
  400.   int quantizer_scale_code;
  401.   int pos;
  402.   int slice_picture_id_enable = 0;
  403.   int slice_picture_id = 0;
  404.   int extra_information_slice = 0;
  405.  
  406.   pos = ld->Bitcnt;
  407.  
  408.   slice_vertical_position_extension =
  409.     (ld->MPEG2_Flag && vertical_size>2800) ? Get_Bits(3) : 0;
  410.  
  411.   if (ld->scalable_mode==SC_DP)
  412.     ld->priority_breakpoint = Get_Bits(7);
  413.  
  414.   quantizer_scale_code = Get_Bits(5);
  415.   ld->quantizer_scale =
  416.     ld->MPEG2_Flag ? (ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1) : quantizer_scale_code;
  417.  
  418.   /* slice_id introduced in March 1995 as part of the video corridendum
  419.      (after the IS was drafted in November 1994) */
  420.   if (Get_Bits(1))
  421.   {
  422.     ld->intra_slice = Get_Bits(1);
  423.  
  424.     slice_picture_id_enable = Get_Bits(1);
  425.     slice_picture_id = Get_Bits(6);
  426.  
  427.     extra_information_slice = extra_bit_information();
  428.   }
  429.   else
  430.     ld->intra_slice = 0;
  431.  
  432. #ifdef VERBOSE
  433.   if (Verbose_Flag>PICTURE_LAYER)
  434.   {
  435.     printf("slice header (byte %d)\n",(pos>>3)-4);
  436.     if (Verbose_Flag>SLICE_LAYER)
  437.     {
  438.       if (ld->MPEG2_Flag && vertical_size>2800)
  439.         printf("  slice_vertical_position_extension=%d\n",slice_vertical_position_extension);
  440.   
  441.       if (ld->scalable_mode==SC_DP)
  442.         printf("  priority_breakpoint=%d\n",ld->priority_breakpoint);
  443.  
  444.       printf("  quantizer_scale_code=%d\n",quantizer_scale_code);
  445.  
  446.       printf("  slice_picture_id_enable = %d\n", slice_picture_id_enable);
  447.  
  448.       if(slice_picture_id_enable)
  449.         printf("  slice_picture_id = %d\n", slice_picture_id);
  450.  
  451.     }
  452.   }
  453. #endif /* VERBOSE */
  454.  
  455. #ifdef VERIFY
  456.   verify_slice_header++;
  457. #endif /* VERIFY */
  458.  
  459.  
  460.   return slice_vertical_position_extension;
  461. }
  462.  
  463.  
  464. /* decode extension and user data */
  465. /* ISO/IEC 13818-2 section 6.2.2.2 */
  466. static void extension_and_user_data()
  467. {
  468.   int code,ext_ID;
  469.  
  470.   next_start_code();
  471.  
  472.   while ((code = Show_Bits(32))==EXTENSION_START_CODE || code==USER_DATA_START_CODE)
  473.   {
  474.     if (code==EXTENSION_START_CODE)
  475.     {
  476.       Flush_Buffer32();
  477.       ext_ID = Get_Bits(4);
  478.       switch (ext_ID)
  479.       {
  480.       case SEQUENCE_EXTENSION_ID:
  481.         sequence_extension();
  482.         break;
  483.       case SEQUENCE_DISPLAY_EXTENSION_ID:
  484.         sequence_display_extension();
  485.         break;
  486.       case QUANT_MATRIX_EXTENSION_ID:
  487.         quant_matrix_extension();
  488.         break;
  489.       case SEQUENCE_SCALABLE_EXTENSION_ID:
  490.         sequence_scalable_extension();
  491.         break;
  492.       case PICTURE_DISPLAY_EXTENSION_ID:
  493.         picture_display_extension();
  494.         break;
  495.       case PICTURE_CODING_EXTENSION_ID:
  496.         picture_coding_extension();
  497.         break;
  498.       case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
  499.         picture_spatial_scalable_extension();
  500.         break;
  501.       case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
  502.         picture_temporal_scalable_extension();
  503.         break;
  504.       case COPYRIGHT_EXTENSION_ID:
  505.         copyright_extension();
  506.         break;
  507.      default:
  508.         fprintf(stderr,"reserved extension start code ID %d\n",ext_ID);
  509.         break;
  510.       }
  511.       next_start_code();
  512.     }
  513.     else
  514.     {
  515. #ifdef VERBOSE
  516.       if (Verbose_Flag>NO_LAYER)
  517.         printf("user data\n");
  518. #endif /* VERBOSE */
  519.       Flush_Buffer32();
  520.       user_data();
  521.     }
  522.   }
  523. }
  524.  
  525.  
  526. /* decode sequence extension */
  527.  
  528. /* ISO/IEC 13818-2 section 6.2.2.3 */
  529. static void sequence_extension()
  530. {
  531.   int horizontal_size_extension;
  532.   int vertical_size_extension;
  533.   int bit_rate_extension;
  534.   int vbv_buffer_size_extension;
  535.  
  536.  
  537.   /* derive bit position for trace */
  538. #ifdef VERBOSE
  539.   pos = ld->Bitcnt;
  540. #endif
  541.  
  542.   ld->MPEG2_Flag = 1;
  543.  
  544.   ld->scalable_mode = SC_NONE; /* unless overwritten by sequence_scalable_extension() */
  545.   layer_id = 0;                /* unless overwritten by sequence_scalable_extension() */
  546.   
  547.   profile_and_level_indication = Get_Bits(8);
  548.   progressive_sequence         = Get_Bits(1);
  549.   chroma_format                = Get_Bits(2);
  550.   horizontal_size_extension    = Get_Bits(2);
  551.   vertical_size_extension      = Get_Bits(2);
  552.   bit_rate_extension           = Get_Bits(12);
  553.   marker_bit("sequence_extension");
  554.   vbv_buffer_size_extension    = Get_Bits(8);
  555.   low_delay                    = Get_Bits(1);
  556.   frame_rate_extension_n       = Get_Bits(2);
  557.   frame_rate_extension_d       = Get_Bits(5);
  558.  
  559.   frame_rate = frame_rate_Table[frame_rate_code] *
  560.     ((frame_rate_extension_n+1)/(frame_rate_extension_d+1));
  561.  
  562.   /* special case for 422 profile & level must be made */
  563.   if((profile_and_level_indication>>7) & 1)
  564.   {  /* escape bit of profile_and_level_indication set */
  565.   
  566.     /* 4:2:2 Profile @ Main Level */
  567.     if((profile_and_level_indication&15)==5)
  568.     {
  569.       profile = PROFILE_422;
  570.       level   = MAIN_LEVEL;  
  571.     }
  572.   }
  573.   else
  574.   {
  575.     profile = profile_and_level_indication >> 4;  /* Profile is upper nibble */
  576.     level   = profile_and_level_indication & 0xF;  /* Level is lower nibble */
  577.   }
  578.   
  579.  
  580.   horizontal_size = (horizontal_size_extension<<12) | (horizontal_size&0x0fff);
  581.   vertical_size = (vertical_size_extension<<12) | (vertical_size&0x0fff);
  582.  
  583.  
  584.   /* ISO/IEC 13818-2 does not define bit_rate_value to be composed of
  585.    * both the original bit_rate_value parsed in sequence_header() and
  586.    * the optional bit_rate_extension in sequence_extension_header(). 
  587.    * However, we use it for bitstream verification purposes. 
  588.    */
  589.  
  590.   bit_rate_value += (bit_rate_extension << 18);
  591.   bit_rate = ((double) bit_rate_value) * 400.0;
  592.   vbv_buffer_size += (vbv_buffer_size_extension << 10);
  593.  
  594. #ifdef VERBOSE
  595.   if (Verbose_Flag>NO_LAYER)
  596.   {
  597.     printf("sequence extension (byte %d)\n",(pos>>3)-4);
  598.  
  599.     if (Verbose_Flag>SEQUENCE_LAYER)
  600.     {
  601.       printf("  profile_and_level_indication=%d\n",profile_and_level_indication);
  602.  
  603.       if (profile_and_level_indication<128)
  604.       {
  605.         printf("    profile=%d, level=%d\n",profile,level);
  606.       }
  607.  
  608.       printf("  progressive_sequence=%d\n",progressive_sequence);
  609.       printf("  chroma_format=%d\n",chroma_format);
  610.       printf("  horizontal_size_extension=%d\n",horizontal_size_extension);
  611.       printf("  vertical_size_extension=%d\n",vertical_size_extension);
  612.       printf("  bit_rate_extension=%d\n",bit_rate_extension);
  613.       printf("  vbv_buffer_size_extension=%d\n",vbv_buffer_size_extension);
  614.       printf("  low_delay=%d\n",low_delay);
  615.       printf("  frame_rate_extension_n=%d\n",frame_rate_extension_n);
  616.       printf("  frame_rate_extension_d=%d\n",frame_rate_extension_d);
  617.     }
  618.   }
  619. #endif /* VERBOSE */
  620.  
  621. #ifdef VERIFY
  622.   verify_sequence_extension++;
  623. #endif /* VERIFY */
  624.  
  625.  
  626. }
  627.  
  628.  
  629. /* decode sequence display extension */
  630.  
  631. static void sequence_display_extension()
  632. {
  633.   int pos;
  634.  
  635.   pos = ld->Bitcnt;
  636.   video_format      = Get_Bits(3);
  637.   color_description = Get_Bits(1);
  638.  
  639.   if (color_description)
  640.   {
  641.     color_primaries          = Get_Bits(8);
  642.     transfer_characteristics = Get_Bits(8);
  643.     matrix_coefficients      = Get_Bits(8);
  644.   }
  645.  
  646.   display_horizontal_size = Get_Bits(14);
  647.   marker_bit("sequence_display_extension");
  648.   display_vertical_size   = Get_Bits(14);
  649.  
  650. #ifdef VERBOSE
  651.   if (Verbose_Flag>NO_LAYER)
  652.   {
  653.     printf("sequence display extension (byte %d)\n",(pos>>3)-4);
  654.     if (Verbose_Flag>SEQUENCE_LAYER)
  655.     {
  656.  
  657.       printf("  video_format=%d\n",video_format);
  658.       printf("  color_description=%d\n",color_description);
  659.  
  660.       if (color_description)
  661.       {
  662.         printf("    color_primaries=%d\n",color_primaries);
  663.         printf("    transfer_characteristics=%d\n",transfer_characteristics);
  664.         printf("    matrix_coefficients=%d\n",matrix_coefficients);
  665.       }
  666.       printf("  display_horizontal_size=%d\n",display_horizontal_size);
  667.       printf("  display_vertical_size=%d\n",display_vertical_size);
  668.     }
  669.   }
  670. #endif /* VERBOSE */
  671.  
  672. #ifdef VERIFY
  673.   verify_sequence_display_extension++;
  674. #endif /* VERIFY */
  675.  
  676. }
  677.  
  678.  
  679. /* decode quant matrix entension */
  680. /* ISO/IEC 13818-2 section 6.2.3.2 */
  681. static void quant_matrix_extension()
  682. {
  683.   int i;
  684.   int pos;
  685.  
  686.   pos = ld->Bitcnt;
  687.  
  688.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  689.   {
  690.     for (i=0; i<64; i++)
  691.     {
  692.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  693.       = ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  694.       = Get_Bits(8);
  695.     }
  696.   }
  697.  
  698.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  699.   {
  700.     for (i=0; i<64; i++)
  701.     {
  702.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  703.       = ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  704.       = Get_Bits(8);
  705.     }
  706.   }
  707.  
  708.   if((ld->load_chroma_intra_quantizer_matrix = Get_Bits(1)))
  709.   {
  710.     for (i=0; i<64; i++)
  711.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  712.   }
  713.  
  714.   if((ld->load_chroma_non_intra_quantizer_matrix = Get_Bits(1)))
  715.   {
  716.     for (i=0; i<64; i++)
  717.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  718.   }
  719.  
  720. #ifdef VERBOSE
  721.   if (Verbose_Flag>NO_LAYER)
  722.   {
  723.     printf("quant matrix extension (byte %d)\n",(pos>>3)-4);
  724.     printf("  load_intra_quantizer_matrix=%d\n",
  725.       ld->load_intra_quantizer_matrix);
  726.     printf("  load_non_intra_quantizer_matrix=%d\n",
  727.       ld->load_non_intra_quantizer_matrix);
  728.     printf("  load_chroma_intra_quantizer_matrix=%d\n",
  729.       ld->load_chroma_intra_quantizer_matrix);
  730.     printf("  load_chroma_non_intra_quantizer_matrix=%d\n",
  731.       ld->load_chroma_non_intra_quantizer_matrix);
  732.   }
  733. #endif /* VERBOSE */
  734.  
  735. #ifdef VERIFY
  736.   verify_quant_matrix_extension++;
  737. #endif /* VERIFY */
  738.  
  739. }
  740.  
  741.  
  742. /* decode sequence scalable extension */
  743. /* ISO/IEC 13818-2   section 6.2.2.5 */
  744. static void sequence_scalable_extension()
  745. {
  746.   int pos;
  747.  
  748.   pos = ld->Bitcnt;
  749.  
  750.   /* values (without the +1 offset) of scalable_mode are defined in 
  751.      Table 6-10 of ISO/IEC 13818-2 */
  752.   ld->scalable_mode = Get_Bits(2) + 1; /* add 1 to make SC_DP != SC_NONE */
  753.  
  754.   layer_id = Get_Bits(4);
  755.  
  756.   if (ld->scalable_mode==SC_SPAT)
  757.   {
  758.     lower_layer_prediction_horizontal_size = Get_Bits(14);
  759.     marker_bit("sequence_scalable_extension()");
  760.     lower_layer_prediction_vertical_size   = Get_Bits(14); 
  761.     horizontal_subsampling_factor_m        = Get_Bits(5);
  762.     horizontal_subsampling_factor_n        = Get_Bits(5);
  763.     vertical_subsampling_factor_m          = Get_Bits(5);
  764.     vertical_subsampling_factor_n          = Get_Bits(5);
  765.   }
  766.  
  767.   if (ld->scalable_mode==SC_TEMP)
  768.     Error("temporal scalability not implemented\n");
  769.  
  770. #ifdef VERBOSE
  771.   if (Verbose_Flag>NO_LAYER)
  772.   {
  773.     printf("sequence scalable extension (byte %d)\n",(pos>>3)-4);
  774.     if (Verbose_Flag>SEQUENCE_LAYER)
  775.     {
  776.       printf("  scalable_mode=%d\n",ld->scalable_mode-1);
  777.       printf("  layer_id=%d\n",layer_id);
  778.       if (ld->scalable_mode==SC_SPAT)
  779.       {
  780.         printf("    lower_layer_prediction_horiontal_size=%d\n",
  781.           lower_layer_prediction_horizontal_size);
  782.         printf("    lower_layer_prediction_vertical_size=%d\n",
  783.           lower_layer_prediction_vertical_size);
  784.         printf("    horizontal_subsampling_factor_m=%d\n",
  785.           horizontal_subsampling_factor_m);
  786.         printf("    horizontal_subsampling_factor_n=%d\n",
  787.           horizontal_subsampling_factor_n);
  788.         printf("    vertical_subsampling_factor_m=%d\n",
  789.           vertical_subsampling_factor_m);
  790.         printf("    vertical_subsampling_factor_n=%d\n",
  791.           vertical_subsampling_factor_n);
  792.       }
  793.     }
  794.   }
  795. #endif /* VERBOSE */
  796.  
  797. #ifdef VERIFY
  798.   verify_sequence_scalable_extension++;
  799. #endif /* VERIFY */
  800.  
  801. }
  802.  
  803.  
  804. /* decode picture display extension */
  805. /* ISO/IEC 13818-2 section 6.2.3.3. */
  806. static void picture_display_extension()
  807. {
  808.   int i;
  809.   int number_of_frame_center_offsets;
  810.   int pos;
  811.  
  812.   pos = ld->Bitcnt;
  813.   /* based on ISO/IEC 13818-2 section 6.3.12 
  814.     (November 1994) Picture display extensions */
  815.  
  816.   /* derive number_of_frame_center_offsets */
  817.   if(progressive_sequence)
  818.   {
  819.     if(repeat_first_field)
  820.     {
  821.       if(top_field_first)
  822.         number_of_frame_center_offsets = 3;
  823.       else
  824.         number_of_frame_center_offsets = 2;
  825.     }
  826.     else
  827.     {
  828.       number_of_frame_center_offsets = 1;
  829.     }
  830.   }
  831.   else
  832.   {
  833.     if(picture_structure!=FRAME_PICTURE)
  834.     {
  835.       number_of_frame_center_offsets = 1;
  836.     }
  837.     else
  838.     {
  839.       if(repeat_first_field)
  840.         number_of_frame_center_offsets = 3;
  841.       else
  842.         number_of_frame_center_offsets = 2;
  843.     }
  844.   }
  845.  
  846.  
  847.   /* now parse */
  848.   for (i=0; i<number_of_frame_center_offsets; i++)
  849.   {
  850.     frame_center_horizontal_offset[i] = Get_Bits(16);
  851.     marker_bit("picture_display_extension, first marker bit");
  852.     
  853.     frame_center_vertical_offset[i]   = Get_Bits(16);
  854.     marker_bit("picture_display_extension, second marker bit");
  855.   }
  856.  
  857. #ifdef VERBOSE
  858.   if (Verbose_Flag>NO_LAYER)
  859.   {
  860.     printf("picture display extension (byte %d)\n",(pos>>3)-4);
  861.     if (Verbose_Flag>SEQUENCE_LAYER)
  862.     {
  863.  
  864.       for (i=0; i<number_of_frame_center_offsets; i++)
  865.       {
  866.         printf("  frame_center_horizontal_offset[%d]=%d\n",i,
  867.           frame_center_horizontal_offset[i]);
  868.         printf("  frame_center_vertical_offset[%d]=%d\n",i,
  869.           frame_center_vertical_offset[i]);
  870.       }
  871.     }
  872.   }
  873. #endif /* VERBOSE */
  874.  
  875. #ifdef VERIFY
  876.   verify_picture_display_extension++;
  877. #endif /* VERIFY */
  878.  
  879. }
  880.  
  881.  
  882. /* decode picture coding extension */
  883. static void picture_coding_extension()
  884. {
  885.   int pos;
  886.  
  887.   pos = ld->Bitcnt;
  888.  
  889.   f_code[0][0] = Get_Bits(4);
  890.   f_code[0][1] = Get_Bits(4);
  891.   f_code[1][0] = Get_Bits(4);
  892.   f_code[1][1] = Get_Bits(4);
  893.  
  894.   intra_dc_precision         = Get_Bits(2);
  895.   picture_structure          = Get_Bits(2);
  896.   top_field_first            = Get_Bits(1);
  897.   frame_pred_frame_dct       = Get_Bits(1);
  898.   concealment_motion_vectors = Get_Bits(1);
  899.   ld->q_scale_type           = Get_Bits(1);
  900.   intra_vlc_format           = Get_Bits(1);
  901.   ld->alternate_scan         = Get_Bits(1);
  902.   repeat_first_field         = Get_Bits(1);
  903.   chroma_420_type            = Get_Bits(1);
  904.   progressive_frame          = Get_Bits(1);
  905.   composite_display_flag     = Get_Bits(1);
  906.  
  907.   if (composite_display_flag)
  908.   {
  909.     v_axis            = Get_Bits(1);
  910.     field_sequence    = Get_Bits(3);
  911.     sub_carrier       = Get_Bits(1);
  912.     burst_amplitude   = Get_Bits(7);
  913.     sub_carrier_phase = Get_Bits(8);
  914.   }
  915.  
  916. #ifdef VERBOSE
  917.   if (Verbose_Flag>NO_LAYER)
  918.   {
  919.     printf("picture coding extension (byte %d)\n",(pos>>3)-4);
  920.     if (Verbose_Flag>SEQUENCE_LAYER)
  921.     {
  922.       printf("  forward horizontal f_code=%d\n", f_code[0][0]);
  923.       printf("  forward vertical f_code=%d\n", f_code[0][1]);
  924.       printf("  backward horizontal f_code=%d\n", f_code[1][0]);
  925.       printf("  backward_vertical f_code=%d\n", f_code[1][1]);
  926.       printf("  intra_dc_precision=%d\n",intra_dc_precision);
  927.       printf("  picture_structure=%d\n",picture_structure);
  928.       printf("  top_field_first=%d\n",top_field_first);
  929.       printf("  frame_pred_frame_dct=%d\n",frame_pred_frame_dct);
  930.       printf("  concealment_motion_vectors=%d\n",concealment_motion_vectors);
  931.       printf("  q_scale_type=%d\n",ld->q_scale_type);
  932.       printf("  intra_vlc_format=%d\n",intra_vlc_format);
  933.       printf("  alternate_scan=%d\n",ld->alternate_scan);
  934.       printf("  repeat_first_field=%d\n",repeat_first_field);
  935.       printf("  chroma_420_type=%d\n",chroma_420_type);
  936.       printf("  progressive_frame=%d\n",progressive_frame);
  937.       printf("  composite_display_flag=%d\n",composite_display_flag);
  938.  
  939.       if (composite_display_flag)
  940.       {
  941.         printf("    v_axis=%d\n",v_axis);
  942.         printf("    field_sequence=%d\n",field_sequence);
  943.         printf("    sub_carrier=%d\n",sub_carrier);
  944.         printf("    burst_amplitude=%d\n",burst_amplitude);
  945.         printf("    sub_carrier_phase=%d\n",sub_carrier_phase);
  946.       }
  947.     }
  948.   }
  949. #endif /* VERBOSE */
  950.  
  951. #ifdef VERIFY
  952.   verify_picture_coding_extension++;
  953. #endif /* VERIFY */
  954. }
  955.  
  956.  
  957. /* decode picture spatial scalable extension */
  958. /* ISO/IEC 13818-2 section 6.2.3.5. */
  959. static void picture_spatial_scalable_extension()
  960. {
  961.   int pos;
  962.  
  963.   pos = ld->Bitcnt;
  964.  
  965.   ld->pict_scal = 1; /* use spatial scalability in this picture */
  966.  
  967.   lower_layer_temporal_reference = Get_Bits(10);
  968.   marker_bit("picture_spatial_scalable_extension(), first marker bit");
  969.   lower_layer_horizontal_offset = Get_Bits(15);
  970.   if (lower_layer_horizontal_offset>=16384)
  971.     lower_layer_horizontal_offset-= 32768;
  972.   marker_bit("picture_spatial_scalable_extension(), second marker bit");
  973.   lower_layer_vertical_offset = Get_Bits(15);
  974.   if (lower_layer_vertical_offset>=16384)
  975.     lower_layer_vertical_offset-= 32768;
  976.   spatial_temporal_weight_code_table_index = Get_Bits(2);
  977.   lower_layer_progressive_frame = Get_Bits(1);
  978.   lower_layer_deinterlaced_field_select = Get_Bits(1);
  979.  
  980. #ifdef VERBOSE
  981.   if (Verbose_Flag>NO_LAYER)
  982.   {
  983.     printf("picture spatial scalable extension (byte %d)\n",(pos>>3)-4);
  984.     if (Verbose_Flag>SEQUENCE_LAYER)
  985.     {
  986.       printf("  lower_layer_temporal_reference=%d\n",lower_layer_temporal_reference);
  987.       printf("  lower_layer_horizontal_offset=%d\n",lower_layer_horizontal_offset);
  988.       printf("  lower_layer_vertical_offset=%d\n",lower_layer_vertical_offset);
  989.       printf("  spatial_temporal_weight_code_table_index=%d\n",
  990.         spatial_temporal_weight_code_table_index);
  991.       printf("  lower_layer_progressive_frame=%d\n",lower_layer_progressive_frame);
  992.       printf("  lower_layer_deinterlaced_field_select=%d\n",lower_layer_deinterlaced_field_select);
  993.     }
  994.   }
  995. #endif /* VERBOSE */
  996.  
  997. #ifdef VERIFY
  998.   verify_picture_spatial_scalable_extension++;
  999. #endif /* VERIFY */
  1000.  
  1001. }
  1002.  
  1003.  
  1004. /* decode picture temporal scalable extension
  1005.  *
  1006.  * not implemented
  1007.  */
  1008. /* ISO/IEC 13818-2 section 6.2.3.4. */
  1009. static void picture_temporal_scalable_extension()
  1010. {
  1011.   Error("temporal scalability not supported\n");
  1012.  
  1013. #ifdef VERIFY
  1014.   verify_picture_temporal_scalable_extension++;
  1015. #endif /* VERIFY */
  1016. }
  1017.  
  1018.  
  1019. /* decode extra bit information */
  1020. /* ISO/IEC 13818-2 section 6.2.3.4. */
  1021. static int extra_bit_information()
  1022. {
  1023.   int Byte_Count = 0;
  1024.  
  1025.   while (Get_Bits1())
  1026.   {
  1027.     Flush_Buffer(8);
  1028.     Byte_Count++;
  1029.   }
  1030.  
  1031.   return(Byte_Count);
  1032. }
  1033.  
  1034.  
  1035.  
  1036. /* ISO/IEC 13818-2 section 5.3 */
  1037. /* Purpose: this function is mainly designed to aid in bitstream conformance
  1038.    testing.  A simple Flush_Buffer(1) would do */
  1039. void marker_bit(char *text)
  1040. {
  1041.   int marker;
  1042.  
  1043.   marker = Get_Bits(1);
  1044.  
  1045. #ifdef VERIFY  
  1046.   if(!marker)
  1047.     printf("ERROR: %s--marker_bit set to 0",text);
  1048. #endif
  1049. }
  1050.  
  1051.  
  1052. /* ISO/IEC 13818-2  sections 6.3.4.1 and 6.2.2.2.2 */
  1053. static void user_data()
  1054. {
  1055.   /* skip ahead to the next start code */
  1056.   next_start_code();
  1057. }
  1058.  
  1059.  
  1060.  
  1061. /* Copyright extension */
  1062. /* ISO/IEC 13818-2 section 6.2.3.6. */
  1063. /* (header added in November, 1994 to the IS document) */
  1064.  
  1065.  
  1066. static void copyright_extension()
  1067. {
  1068.   int pos;
  1069.   int reserved_data;
  1070.  
  1071.   pos = ld->Bitcnt;
  1072.   
  1073.  
  1074.   copyright_flag =       Get_Bits(1); 
  1075.   copyright_identifier = Get_Bits(8);
  1076.   original_or_copy =     Get_Bits(1);
  1077.   
  1078.   /* reserved */
  1079.   reserved_data = Get_Bits(7);
  1080.  
  1081.   marker_bit("copyright_extension(), first marker bit");
  1082.   copyright_number_1 =   Get_Bits(20);
  1083.   marker_bit("copyright_extension(), second marker bit");
  1084.   copyright_number_2 =   Get_Bits(22);
  1085.   marker_bit("copyright_extension(), third marker bit");
  1086.   copyright_number_3 =   Get_Bits(22);
  1087.  
  1088.   if(Verbose_Flag>NO_LAYER)
  1089.   {
  1090.     printf("copyright_extension (byte %d)\n",(pos>>3)-4);
  1091.     if (Verbose_Flag>SEQUENCE_LAYER)
  1092.     {
  1093.       printf("  copyright_flag =%d\n",copyright_flag);
  1094.         
  1095.       printf("  copyright_identifier=%d\n",copyright_identifier);
  1096.         
  1097.       printf("  original_or_copy = %d (original=1, copy=0)\n",
  1098.         original_or_copy);
  1099.         
  1100.       printf("  copyright_number_1=%d\n",copyright_number_1);
  1101.       printf("  copyright_number_2=%d\n",copyright_number_2);
  1102.       printf("  copyright_number_3=%d\n",copyright_number_3);
  1103.     }
  1104.   }
  1105.  
  1106. #ifdef VERIFY
  1107.   verify_copyright_extension++;
  1108. #endif /* VERIFY */
  1109. }
  1110.  
  1111.  
  1112.  
  1113. /* introduced in September 1995 to assist Spatial Scalability */
  1114. static void Update_Temporal_Reference_Tacking_Data()
  1115. {
  1116.   static int temporal_reference_wrap  = 0;
  1117.   static int temporal_reference_old   = 0;
  1118.  
  1119.   if (ld == &base)            /* *CH* */
  1120.   {
  1121.     if (picture_coding_type!=B_TYPE && temporal_reference!=temporal_reference_old)     
  1122.     /* check first field of */
  1123.     {                            
  1124.        /* non-B-frame */
  1125.       if (temporal_reference_wrap)         
  1126.       {/* wrap occured at previous I- or P-frame */    
  1127.        /* now all intervening B-frames which could 
  1128.           still have high temporal_reference values are done  */
  1129.         Temporal_Reference_Base += 1024;
  1130.         temporal_reference_wrap = 0;
  1131.       }
  1132.       
  1133.       /* distinguish from a reset */
  1134.       if (temporal_reference<temporal_reference_old && !Temporal_Reference_GOP_Reset)    
  1135.         temporal_reference_wrap = 1;  /* we must have just passed a GOP-Header! */
  1136.       
  1137.       temporal_reference_old = temporal_reference;
  1138.       Temporal_Reference_GOP_Reset = 0;
  1139.     }
  1140.  
  1141.     True_Framenum = Temporal_Reference_Base + temporal_reference;
  1142.     
  1143.     /* temporary wrap of TR at 1024 for M frames */
  1144.     if (temporal_reference_wrap && temporal_reference <= temporal_reference_old)    
  1145.       True_Framenum += 1024;                
  1146.  
  1147.     True_Framenum_max = (True_Framenum > True_Framenum_max) ?
  1148.                         True_Framenum : True_Framenum_max;
  1149.   }
  1150. }
  1151.